<?php	require_once(dirname(__FILE__).'/inc/config.inc.php');

/*
**************************
(C)2010-2014 phpMyWind.com
update: 2014-5-22 21:58:57
person: Feng
**************************
*/


//初始化参数
$action = isset($action) ? $action : '';

//切换安全标识
if($action == 'GetRandStr')
{	
	echo GetRandStr(16);
	exit();
}

//设置当前站点
if($action == 'selsite')
{
	if(isset($sitekeyvalue))
	{
		$r = $dosql->GetOne("SELECT `id`,`sitekey` FROM `#@__site` WHERE `sitekey`='$sitekeyvalue'");
		if(isset($r['id']))
		{
			$_SESSION['siteid']  = $r['id'];
			$_SESSION['sitekey'] = $r['sitekey'];
		}
		else
		{
			$r = $dosql->GetOne("SELECT `id`,`sitekey` FROM `#@__site` ORDER BY `id` ASC");
			if(isset($r['id']))
			{
				$_SESSION['siteid']  = $r['id'];
				$_SESSION['sitekey'] = $r['sitekey'];
			}
			else
			{
				$_SESSION['siteid']  = '';
				$_SESSION['sitekey'] = '';
			}
		}
	}

	//大后台不刷新左侧菜单
	if(!empty($_SESSION['t_adminlevel']))
		echo 2;
	else if($_SESSION['adminlevel'] == 1)
		echo 1;
	else
		echo 2;
	
	exit();
}


//设置当前权限
if($action == 'selpriv')
{
	//超级管理员才可以切换权限
	if($_SESSION['adminlevel'] == 1 && !empty($privid))
	{
		//第一次进行切换
		if(empty($_SESSION['t_adminlevel']) &&
		   $privid == 1)
		{
			echo 2;
		}

		//如果进行过切换，但本次切换和上次切换相同
		else if(!empty($_SESSION['t_adminlevel']) &&
		   $_SESSION['t_adminlevel'] == $privid)
		{
			echo 2;
		}

		//如果进行过切换，本次为新的切换
		else if(!empty($_SESSION['t_adminlevel']) &&
		   $_SESSION['t_adminlevel'] != $privid)
		{
			$_SESSION['t_adminlevel'] = $privid;

			if($privid == 1)
			{
				$_SESSION['t_adminlevel'] = '';
				unset($_SESSION['t_adminlevel']);
			}

			echo 1;
		}
		else
		{
			$_SESSION['t_adminlevel'] = $privid;
			
			echo 1;
		}
	}

	exit();
}


//网站信息配置
//删除登陆背景
if($action == 'delloginbg')
{
	if(isset($delloginbg))
	{
		unlink(dirname(__FILE__).'/'.$delloginbg);
	}

	echo TRUE;
	exit();
}


//修改单页信息
//单页二级类别
if($action == 'infomaintype')
{
	IsModelPriv('info');

	$r = $dosql->GetOne("SELECT * FROM `#@__info` WHERE `classid`=$classid AND `mainid`=$mainid");
	if(is_array($r))
		echo $r['content'].'[-||-]'.$r['picurl'].'[-||-]'.GetDateTime($r['posttime']);
	else
		echo '[-||-][-||-]'.GetDateTime(time());

	exit();
}


//自定义字段
//栏目选择权限
if($action == 'diyfield')
{
	IsModelPriv('diyfield_list');

	$type = isset($type) ? $type : '';
	echo '<span>';
	$dosql->Execute("SELECT * FROM `#@__infoclass` WHERE `infotype`='$type' ORDER BY orderid ASC");
	if($dosql->GetTotalRow())
	{	
		while($row = $dosql->GetArray())
		{	
			echo '<input type="checkbox" name="classid[]" id="classid[]'.$row['id'].'" value="'.$row['id'].'" /><label for="classid[]'.$row['id'].'">'.$row['classname'].'</label>';
		}
		
	}
	else
	{
		echo '<input type="checkbox" id="noclass" disabled="disabled" value="" /><label for="noclass">该模型下暂无栏目<label/>';
	
	}
	echo '</span><script type="text/javascript" charset="utf-8" src="templates/js/common.js"></script>';
	exit();
}

//自定义字段
//栏目选择权限
if($action == 'fieldtype')
{
	IsModelPriv('diyfield_list');

	$type = isset($type) ? $type : '';
		
		switch($type)
			{
				case 'varchar':
					echo' <dl>
							<dt>样式</dt>
							<dd>
							  <div class="rule-multi-radio">
								<span id="fieldclass"><input id="checkinfo_1" type="radio" name="fieldclass" value="normal" checked="checked" /><label for="checkinfo_1">长(300px)</label>
								<input id="fieldclass_2" type="radio" name="fieldclass" value="short" /><label for="checkinfo_2">中(150px)</label>
								<input id="fieldclass_3" type="radio" name="fieldclass" value="small" /><label for="checkinfo_3">短(50px)</label>
								</span>
							  </div>
							</dd>
						  </dl>
						  <dl>
							<dt>字段长度</dt>
							<dd>
							<input type="text" name="fieldlong" id="fieldlong" datatype="n1-3" class="input small" />
							<span class="Validform_checktip">*长度小于或等于255</span>
							</dd>
						 </dl>
						 <dl>
							<dt>默认值</dt>
							<dd>
								<input type="text" name="fieldsel" id="fieldsel" class="input normal" />
								<span class="Validform_checktip">*字段的默认字符，可为空</span>
							</dd>
						</dl>  
						<script type="text/javascript" charset="utf-8" src="templates/js/common.js"></script>';
					break;
				case 'mediumtext':
					echo'
						<dl>
							<dt>编辑器类型</dt>
							<dd>
							  <div class="rule-multi-radio">
								<span id="fieldclass"><input id="fieldclass_1" type="radio" name="fieldclass" value="mediumtext" checked="checked" /><label for="checkinfo_1">标准型</label>
								<input id="fieldclass_2" type="radio" name="fieldclass" value="mediumtextmini" /><label for="checkinfo_2">简洁型</label>
								</span>
							  </div>
							</dd>
						  </dl>
						   <dl>
							<dt>默认值</dt>
							<dd>
								<input type="text" name="fieldsel" id="fieldsel" class="input normal" />
								<span class="Validform_checktip">*字段的默认字符，可为空</span>
							</dd>
						</dl>  
						<script type="text/javascript" charset="utf-8" src="templates/js/common.js"></script>';
					break;
				case 'number':
					echo'
						<dl>
							<dt>字段类型</dt>
							<dd>
							  <div class="rule-multi-radio">
								<span id="fieldclass"><input id="fieldclass_1" type="radio" name="fieldclass" value="int" checked="checked" /><label for="checkinfo_1">整数[11]</label>
								<input id="fieldclass_2" type="radio" name="fieldclass" value="decimal" /><label for="checkinfo_2">小数[10,2]</label>
								</span>
							  </div>
							  <span class="Validform_checktip">*[11] 和 [10,2] 为默认字段长度</span>
							</dd>
						  </dl>
						    <dl>
							<dt>字段长度</dt>
							<dd>
							<input type="text" name="fieldlong" id="fieldlong" datatype="*" class="input small" />
							<span class="Validform_checktip">*整数 如123456,长度默认为&quot;11&quot;| 小数如102.56 长度默认为&quot;10,2&quot;，&quot;10&quot;代表值总长度，&quot;2&quot;代表小数位数 </span>
							</dd>
						 </dl>
						  <dl>
							<dt>默认值</dt>
							<dd>
								<input type="text" name="fieldsel" id="fieldsel" class="input normal" />
								<span class="Validform_checktip">*字段的默认字符，可为空</span>
							</dd>
						</dl>  
						<script type="text/javascript" charset="utf-8" src="templates/js/common.js"></script>';
					break;	
					case 'checkbox':
					echo'
						   <dl>
							<dt>字段长度</dt>
							<dd>
							<input type="text" name="fieldlong" id="fieldlong" datatype="*" class="input small" />
							<span class="Validform_checktip">*认长度小于或等于 "255" </span>
							</dd>
						 </dl>
						  <dl>
							<dt>多选选项</dt>
							<dd>
								<textarea name="fieldsel" id="fieldsel" datatype="*"  class="input" ></textarea>
								<span class="Validform_checktip">*格式：&quot;选项=值&quot;，每个选项用&quot;,&quot;分割</span>
							</dd>
						</dl>  
						';
					break;
					case 'radio':
					echo'
						   <dl>
							<dt>字段长度</dt>
							<dd>
							<input type="text" name="fieldlong" id="fieldlong" datatype="*" class="input small" />
							<span class="Validform_checktip">*认长度小于或等于 "255" </span>
							</dd>
						 </dl>
						  <dl>
							<dt>单选选项</dt>
							<dd>
								<textarea name="fieldsel" id="fieldsel" datatype="*"  class="input" ></textarea>
								<span class="Validform_checktip">*格式：&quot;选项=值&quot;，每个选项用&quot;,&quot;分割</span>
							</dd>
						</dl>  
						';
					break;	
					case 'select':
					echo'
						   <dl>
							<dt>字段长度</dt>
							<dd>
							<input type="text" name="fieldlong" id="fieldlong" datatype="*" class="input small" />
							<span class="Validform_checktip">*认长度小于或等于 "255" </span>
							</dd>
						 </dl>
						  <dl>
							<dt>下拉选项</dt>
							<dd>
								<textarea name="fieldsel" id="fieldsel" datatype="*"  class="input" ></textarea>
								<span class="Validform_checktip">*格式：&quot;选项=值&quot;，每个选项用&quot;,&quot;分割</span>
							</dd>
						</dl>  
						';
					break;	
					case 'file':
					echo'
						 <dl>
							<dt>上传类型</dt>
							<dd>
							  <div class="rule-multi-radio">
								<span id="fieldclass"><input id="fieldclass_1" type="radio" name="fieldclass" value="all" checked="checked" /><label for="checkinfo_1">全部</label>
								<input id="fieldclass_2" type="radio" name="fieldclass" value="image" /><label for="checkinfo_2">图片</label>
								<input id="fieldclass_3" type="radio" name="fieldclass" value="soft" /><label for="checkinfo_3">软件</label>
								<input id="fieldclass_4" type="radio" name="fieldclass" value="media" /><label for="checkinfo_4">媒体</label>
								</span>
							  </div>
							  <span class="Validform_checktip"></span>
							</dd>
						  </dl><script type="text/javascript" charset="utf-8" src="templates/js/common.js"></script>
						';
					break;
					case 'fileall':
					echo'
						 <dl>
							<dt>上传类型</dt>
							<dd>
							  <div class="rule-multi-radio">
								<span id="fieldclass"><input id="fieldclass_1" type="radio" name="fieldclass" value="all" checked="checked" /><label for="checkinfo_1">全部</label>
								<input id="fieldclass_2" type="radio" name="fieldclass" value="image" /><label for="checkinfo_2">图片</label>
								<input id="fieldclass_3" type="radio" name="fieldclass" value="soft" /><label for="checkinfo_3">软件</label>
								<input id="fieldclass_4" type="radio" name="fieldclass" value="media" /><label for="checkinfo_4">媒体</label>
								</span>
							  </div>
							  <span class="Validform_checktip"></span>
							</dd>
							<dl>
							<dt>上传数量</dt>
							<dd>
							<input type="text" name="fieldsel" id="fieldsel" value="50" class="input small" />
							<span class="Validform_checktip">*默认单次上传最多50个文件 </span>
							</dd>
						 </dl>
						  </dl><script type="text/javascript" charset="utf-8" src="templates/js/common.js"></script>
						';
					break;
				default:
				echo '  <dl>
							<dt>默认值</dt>
							<dd>
								<input type="text" name="fieldsel" id="fieldsel" class="input normal" />
								<span class="Validform_checktip">*字段的默认字符，可为空</span>
							</dd>
						</dl>  ';
			}

	exit();
}



//添加信息自定义字段
//添加信息权限
if($action == 'infoclass')
{
	$id  = isset($id)  ? $id  : 0;
	$row = isset($row) ? $row : '';


	$r = $dosql->GetOne("SELECT `infotype` FROM `#@__infoclass` WHERE `id`=$id");
	if(isset($r['infotype']))
		$type = $r['infotype'];
	else
		$type = '';


	echo GetDiyField($type, $id, $row);
	exit();
}

//属性选择
//添加信息权限
if($action == 'flag')
{
	$id  = isset($id)  ? $id  : 0;
	$flag = isset($flag) ? $flag : '';

	echo GetFlagClass($id,$flag);
	exit();
}


//栏目管理
//获取栏目缩略图大小
if($action == 'catpsize')
{
	IsModelPriv('infoclass');

	$str = '';

	$r = $dosql->GetOne("SELECT `picwidth`,`picheight` FROM `#@__infoclass` WHERE `id`=".$pid);
	if(isset($r['picwidth']))
		$str .= $r['picwidth'];
	
	if(isset($r['picheight']))
		$str .= '|'.$r['picheight'];
	
	echo $str;
	exit();
}


//会员管理
//检测用户是否存在
if($action == 'checkuser')
{
	IsModelPriv('member');

	$r = $dosql->GetOne("SELECT `username` FROM `#@__member` WHERE username='$username'");

	if(!is_array($r))
		echo '<span class="reok">可以使用</span>';
	else
		echo '<span class="renok">用户名已存在</span>';
	exit();
}

//管理员添加
//检测用户是否存在
if($action == 'checkadmin')
{
	IsModelPriv('admin');
	$username =$_POST["param"];
	$r = $dosql->GetOne("SELECT `username` FROM `#@__admin` WHERE username='$username'");

	if(!is_array($r))
		echo '{"info":"","status":"y"}';
	else
		echo '用户名已存在';
	exit();
}

//后台菜单
//检测别名是否存在
if($action == 'navigation')
{
	$username = $_REQUEST["param"];
	$old_name = isset($_REQUEST['old_name']) ? $_REQUEST['old_name'] : '';
	$r = $dosql->GetOne("SELECT `idname` FROM `#@__navigation` WHERE idname='$username'");
	if(!is_array($r))
		echo '{"info":"","status":"y"}';
	else if(@$old_name==$username)
		echo '{"info":"该导航别名可使用","status":"y"}';
	else
		echo '别名已存在';
	exit();
}
//拓展字段
//检测拓展字段标示是否存在
if($action == 'diyfieldname')
{
	$fieldname = $_REQUEST["param"];
	$old_name = isset($_REQUEST['old_name']) ? $_REQUEST['old_name'] : '';
	$r = $dosql->GetOne("SELECT `fieldname` FROM `#@__diyfield` WHERE fieldname='$fieldname'");

	if(!is_array($r))
		echo '{"info":"","status":"y"}';
	else if($old_name==$fieldname)
		echo '{"info":"该导航别名可使用","status":"y"}';
	else
		echo '标示已存在';
	exit();
}
//会员拓展字段
	/**
	 * 检查字段是否存在
	 */
 if($action == 'public_checkfield')
{
		$old_name = isset($_REQUEST['old_name']) ? $_REQUEST['old_name'] : '';
		$field = strtolower($_REQUEST["param"]);
		$oldfield = strtolower($old_name);
		if($field==$oldfield) exit('{"info":"该导航别名可使用","status":"y"}');
		
		$fields = $dosql->get_fields($tbname);
	
		if(array_key_exists($field, $fields)) {
			echo '标示已存在';
		} else {
			echo '{"info":"该导航别名可使用","status":"y"}';
		}
	exit();
}
//会员管理
//获取级联
if($action == 'getarea')
{
	$datagroup = isset($datagroup) ? $datagroup : '';
	$level     = isset($level)     ? $level     : '';
	$v         = isset($areaval)   ? $areaval   : '0';


	$str = '<div class="rule-single-select" >
		<select name="address_'.$name.'" ><option value="-1">--</option>';
	$sql = "SELECT * FROM `#@__cascadedata` WHERE level=$level And ";

	if($v == 0)
		$sql .= "datagroup='$datagroup'";
	else if($v % 500 == 0)
		$sql .= "datagroup='$datagroup' AND datavalue>$v AND datavalue<".($v + 500);
	else
		$sql .= "datavalue LIKE '$v.%%%' AND datagroup='$datagroup'";
	
	$sql .= " ORDER BY orderid ASC, datavalue ASC";


	$dosql->Execute($sql);
	while($row = $dosql->GetArray())
	{
		$str .= '<option value="'.$row['datavalue'].'">'.$row['dataname'].'</option>';
	}
	
	if($str == '') $str .= '<option value="-1">--</option>'; 
	$str .='</select></div><script type="text/javascript" charset="utf-8" src="templates/js/common.js"></script>';
	echo $str;
	exit();
}


//商品信息管理
//获取商品属性
if($action == 'goodsattr')
{
	IsModelPriv('goods');


	if($tid != '-1')
	{
		$dosql->Execute("SELECT * FROM `#@__goodsattr` WHERE `goodsid`=$tid");
		if($dosql->GetTotalRow() > 0)
		{
			$i = 1;
			while($row = $dosql->GetArray())
			{

	 			echo '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
					  <td width="25%" height="40" align="right">'.$row['attrname'].'：</td>
					  <td><input type="text" name="attrvalue[]" id="attrvalue[]" class="input" />
					      <input type="hidden" name="attrid[]" id="attrid[]" value="'.$row['id'].'">';
				
				if($i <= 1)
					echo '<span class="cnote">不同属性值用 <span class="maroon2">|</span> 隔开，例如：黑色|白色 等</span>';
				
				echo '</td></tr></table>';

				$i++;
			}
		}
		else
		{
			echo '<div style="text-align:center;color:#9C0;">暂无自定义属性，您可以在商品类别中进行绑定</div>';
		}
	}
	else
	{
		echo '<div style="text-align:center;color:#9C0;">请选择商品类别获取自定义属性</div>';
	}
	
	exit();
}


//信息模块
//回收站
if($action == 'recycel' or $action == 'reset' or $action == 'del' or
   $action == 'resetall' or $action == 'delall' or $action == 'empty')
{
	if(isset($type))
	{
		$tbname = '#@__'.$type;
	}
	else
	{
		echo '参数错误，没有获取到表名称';
		exit();
	}
	
	
	//选择执行操作
	switch($action)
	{
		case 'reset':
			$sql = "UPDATE `$tbname` SET delstate='', deltime=0 WHERE id=$id";
			$dosql->ExecNoneQuery($sql);
			break;
	
		case 'del':
			$sql = "DELETE FROM `$tbname` WHERE id=$id";
			$dosql->ExecNoneQuery($sql);
			break;
	
		case 'resetall':
			$sql = "UPDATE `$tbname` SET delstate='', deltime=0 WHERE id IN ($ids)";
			$dosql->ExecNoneQuery($sql);
			break;
	
		case 'delall':
			$sql = "DELETE FROM `$tbname` WHERE id IN ($ids)";
			$dosql->ExecNoneQuery($sql);
			break;
	
		case 'empty':
			$sql = "DELETE FROM `$tbname` WHERE delstate='true'";
			$dosql->ExecNoneQuery($sql);
			break;
		default:
	}
	
	//Ajax输出数据
	$dosql->Execute("SELECT * FROM `$tbname` WHERE delstate='true' ORDER BY deltime DESC");
	
	if($dosql->GetTotalRow() == 0)
	{
		echo '暂无内容';
		exit();
	}
	else
	{
		while($row = $dosql->GetArray())
		{
		 
			$r = $dosql->GetOne("SELECT `classname` FROM `#@__infoclass` WHERE id=".$row['classid']);
			if(isset($r['classname']))
				$classname = $r['classname'].' ['.$row['classid'].']';
			else
				$classname = '分类已删 ['.$row['classid'].']';

			 echo '<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" class="dataTable">
					<tr align="left" class="dataTr" onmouseover="this.className=\'dataTrOn\'" onmouseout="this.className=\'dataTr\'">
					 <td width="30" height="28" class="firstCol"><input type="checkbox" name="checkid[]" id="checkid[]" value="'.$row['id'].'" /></td>
					 <td width="30">'.$row['id'].'</td>
					 <td><span class="title" title="删除日期：'.GetDateTime($row['deltime'])."\n".'所属栏目：'.$classname.'">'.$row['title'].'</span></td>
					 <td width="90" class="action endCol"><span><a href="javascript:;" onclick="RecycleRe(\'reset\','.$row['id'].')">还原</a></span><span class="nb"><a href="javascript:;" onclick="RecycleRe(\'del\','.$row['id'].')">删除</a></span></td>
			  	 </tr>
		      	</table>';
		}

		exit();
	}
}


//管理首页
//是否保存便签
if($action == 'adminnotes')
{
	$uname    = $_SESSION['admin'];
	$body     = trim($body);
	$posttime = time();
	$postip   = GetIP();

	if($dosql->GetOne("SELECT `uname` FROM `#@__adminnotes` WHERE uname='$uname'"))
	{
		$sql = "UPDATE `#@__adminnotes` SET body='$body', posttime='$posttime', postip='$postip' WHERE uname='$uname'";
		$dosql->ExecNoneQuery($sql);
	}
	else
	{
		$sql = "INSERT INTO `#@__adminnotes` (uname, body, posttime, postip) VALUES ('$uname', '$body', '$posttime', '$postip')";
		$dosql->ExecNoneQuery($sql);
	}
	
	exit();
}


//删除记事本
if($action == 'deladminnotes')
{
	$sql = "DELETE FROM `#@__adminnotes` WHERE `uname`='".$_SESSION['admin']."'";
	$dosql->ExecNoneQuery($sql);
	exit();
}

//更新排序
if($action == 'diyfieldorderid')
{	
	$id    = $pid;
	$orderid  = trim($orderid);

	$sql = "UPDATE `#@__diyfield` SET orderid='$orderid' WHERE id='$id'";
	$dosql->ExecNoneQuery($sql);
	exit();
}
//更新会员字段排序
if($action == 'userfieldorderid')
{	
	$id    = $pid;
	$orderid  = trim($orderid);

	$sql = "UPDATE `#@__userfield` SET orderid='$orderid' WHERE id='$id'";
	$dosql->ExecNoneQuery($sql);
	exit();
}


//生成验证缓存
if($action == 'updataauth')
{
	$fdir  = PHPMYWIND_DATA.'/cache/auth/';
	$fname = 'auth_'.$cfg_auth_key.'.php';


	//是否存在缓存
	Writef($fdir.$fname, $jsonStr);


	echo TRUE;
	exit();
}


//切换访问设备
if($action = 'selsiteeq')
{
	$eq = isset($eq) ? $eq : 'pc';
	
	if($eq == 'pc')
		$_SESSION['siteeq'] = 'pc';
	else if($eq == 'mobile')
		$_SESSION['siteeq'] = 'mobile';
	else
		$_SESSION['siteeq'] = 'pc';
	
	echo TRUE;
	exit();
}




//无条件返回
else
{
	exit('Request Error!');
}
?>